﻿using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using Cratos.Generator.DataObject;
using Cratos.Tools.General;
using Cratos.Desktop;

namespace Cratos.Generator
{
	/// <summary>
	/// Project Presenter class
	/// </summary>
	public class OpenProjectPresenter : MVPCrudPresenter<OpenProjectView>
	{
		#region Class Constant declarations
		#endregion

		#region Class Event declarations
		#endregion

		#region Class Variable declarations
		private ApplicationPresenter						m_AppPresenter;
		private DOCratosProject												m_Project;
		private ObservableCollection<DOCratosProject> m_AllProjects;	
		#endregion

		#region Class Constructors & Destructors
		/// <summary>
		/// Class Constructor
		/// </summary>
		public OpenProjectPresenter(ApplicationPresenter appPresenter, OpenProjectView currentView) : base(currentView, "Open Project")
		{
			// Create the relevant objects
			this.m_Project     = new DOCratosProject();
			this.m_AllProjects = new ObservableCollection<DOCratosProject>();

			// Save the relevant values
			this.m_AppPresenter		= appPresenter;
			currentView.Presenter	= this;
		}
		#endregion

		#region Class Properties
		/// <summary>
		/// Current Project
		/// </summary>
		public DOCratosProject Project
		{
			get { return (this.m_Project); }
			set { this.m_Project = value; }
		}

		/// <summary>
		/// Application Presenter
		/// </summary>
		public ApplicationPresenter ApplicationPresenter
		{
			get { return (this.m_AppPresenter); }
		}

		/// <summary>
		/// All Projects
		/// </summary>
		public ObservableCollection<DOCratosProject> AllProjects
		{
			get { return (this.m_AllProjects); }
		}
		#endregion

		#region Class Methods
		/// <summary>
		/// Validate the relevant object
		/// </summary>
		/// <returns>A boolean value indicating the success of the method</returns>
		public bool Validate()
		{
			// Declare the required variables
			bool returnValue = true;

			// Clear all pending errors
			this.Errors.Clear();

			// Validate the relevant fields
			if (string.IsNullOrEmpty(this.Project.Name))
			{
				this.Errors.Add(new CratosError("Open Project", CratosLogSeverity.ValidationError, "Project Name is mandatory"));
				returnValue = false;
			}

			// return to calling method
			return (returnValue);
		}

		/// <summary>
		/// Load All Lookups
		/// </summary>
		/// <returns>A boolean value indicating the success of the method</returns>
		public bool LoadAllProjects()
		{
			// Declare the required variables
			bool								returnValue = true;
			DOCratosProjectCollection	allProjects;

			//// Reset the Transaction Timer
			//this.TransactionTime = 0;

			try
			{
			  // Start the progress
			  this.OnProgressStart();

			  // Update the Progress
			  this.OnProgressUpdate("Preparing to send request to server");

			  // Clear all current items
			  this.AllProjects.Clear();

				// Create and setup the relevant objects
				allProjects                                     = new DOCratosProjectCollection();
				allProjects.CollectionAction                    = DOCratosCollectionAction.Retrieve;
				allProjects.DataObject.Modules.CollectionAction = DOCratosCollectionAction.Retrieve;

				// Update the Progress
				this.OnProgressUpdate("Sending request to server");

				// Send the request to the server
				if (allProjects.PerformAction())
				{
					// Update the Progress
					this.OnProgressUpdate("Processing response from server");

					// Make sure we have a valid response from server
					if (allProjects.CollectionActionResult == DOCratosActionResult.Success)
					{
						foreach (DOCratosProject currentItem in allProjects) { this.AllProjects.Add(currentItem); }
					}

					// Raise the relevant events
					this.OnNotifyPropertyChanged("AllProjects");
				}
				else
				{
					// Update the errors
					foreach (CratosError currentError in allProjects.Errors) { this.Errors.Add(currentError); }
				}
			}
			catch (Exception runtimeException)
			{
				// Display the exception
				CratosHelper.LogMessage(CratosLogSeverity.Exception, "Open Project [LoadAllProjects]",
																"Unhandled exception occurred", runtimeException, true);
			}
			finally
			{
				// Stop the Progress
				this.OnProgressEnd();

				// Set the return value
				returnValue = (this.Errors.Count <= 0);
			}

			// Display the error message if required
			if (this.Errors.Count > 0) { this.DisplayError("Open Project"); }

			// return to calling method
			return (returnValue);
		}

		/// <summary>
		/// Open the Selected Project
		/// </summary>
		public void OpenProject()
		{
			// Declare the required variables

			// Make sure we have a Project selected
			if (this.Project.Id > 0)
			{
				// Open the selected Project
				this.ApplicationPresenter.OpenProject(this.Project.Id);

				// Close the current View
				this.Close();
			}

			// Return to calling method
			return;
		}

		/// <summary>
		/// Close the Tab
		/// </summary>
		public void Close()
		{
			// Declare the required variables

			// Close the Tab
			this.View.Close();

			// Return to calling method
			return;
		}
		#endregion

		#region Class Support Methods
		#endregion
	}
}
